package com.abei.gas.service.Impl;

import com.abei.gas.entity.Card;
import com.abei.gas.entity.Device;
import com.abei.gas.mapper.DeviceMapper;
import com.abei.gas.service.DeviceService;
import com.abei.gas.vo.DataVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
    @Resource
    private DeviceMapper deviceMapper;
    @Override
    public DataVo<Device> findAll(Integer page,Integer limit) {
        List<Device> c=deviceMapper.selectList(null);
        DataVo<Device> datavo=new DataVo<>();
        datavo.setCode(0);
        datavo.setMsg("");
        IPage<Device> cardpage=new Page<>(page,limit);
        IPage<Device> result=deviceMapper.selectPage(cardpage,null);
        datavo.setCount(result.getTotal());
        c=result.getRecords();
        datavo.setData(c);
        return datavo;
    }
    @Override
    public DataVo<Device> findById(String number) {
        QueryWrapper<Device> u = new QueryWrapper<>();
        u.like("id",number);
        List<Device> cardList=deviceMapper.selectList(u);
        DataVo<Device> dataVO=new DataVo<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        Integer num = deviceMapper.selectCount(null);
        dataVO.setCount(Long.valueOf(num));
        dataVO.setData(cardList);
        return dataVO;
    }

    @Override
    public void deleteById(String deviceid) {
        Map<String,Object> columMap=new HashMap<>();
        columMap.put("id",deviceid);
        int i=deviceMapper.deleteByMap(columMap);
        System.out.println(i);
    }

    @Override
    public void Insert(Device device) {
        Device u=new Device();
        u.setId(device.getId());
        u.setTime(device.getTime());
        u.setBrand(device.getBrand());
        u.setStatus(device.getStatus());
        u.setUserid(device.getUserid());
        deviceMapper.insert(u);
    }

    @Override
    public void Update(String number, Device device) {
        QueryWrapper<Device> u=new QueryWrapper<Device>();
        u.eq("id",number);
        deviceMapper.update(device,u);
    }

    @Override
    public void DeleteBatchIds(String ids) {
        String[] sArray=ids.split("&");
        List<String> id= new ArrayList<>();
        for (String i:sArray
        ) {
            id.add(i.substring(4));
        }
        int i = deviceMapper.deleteBatchIds(id);
        System.out.println("影响的行数：" + i);
    }
}
